using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml.Serialization;
using System.IO;

namespace TPDSControl
{
    [Serializable()]
    [XmlInclude(typeof(Image))]
    public class ImageSet
    {
        [XmlElement("Image")]
        public List<Image> Images;
        [XmlIgnore()]
        public string Filepath;
        public string BarcodeOutputFile;

        public ImageSet()
        {
            Images = new List<Image>();
        }

        public bool IsProcessed
        {
            get 
            {
                foreach (Image img in Images)
                {
                    if (img.Status != Image.ImageStatus.Processed)
                    {
                        return false;
                    }
                }
                return true;
            }
        }

        public static ImageSet LoadOne(string filepath)
        {
            try
            {
                ImageSet imageset = (ImageSet)TPDSConfig.Utils.LoadXML(filepath, typeof(ImageSet));
                if( imageset != null )
                {
                    imageset.Filepath = filepath;

                    // Always ensure the image objects known the file name of the imageset they belong to.
                    foreach( TPDSControl.Image image in imageset.Images )
                        image.ImagesetFilepath = imageset.Filepath;
                }

                return imageset;
            }
            catch (Exception ex)
            {
                TPDSConfig.ErrorLog.WriteError(ex.Message, filepath);
            }
            return null;
        }

        public static List<ImageSet> LoadAll(string folderpath)
        {
            List<ImageSet> list = new List<ImageSet>();
            try
            {
                DirectoryInfo folder = new DirectoryInfo(folderpath);
                FileInfo[ ] files = folder.GetFiles( "*.set" );

                foreach (FileInfo file in files)
                {
                    ImageSet imageset = LoadOne(file.FullName);

                    //Only load imagesets that contain data
                    if( imageset != null && imageset.Images.Count > 0) 
                        list.Add(imageset);
                }
                return list;
            }
            catch (Exception ex)
            {
                TPDSConfig.ErrorLog.WriteError(ex.Message, folderpath);
            }

            // Return empty imageset list.
            return list;
        }

        public void Save()
        {
            TPDSConfig.Utils.SaveXML(Filepath, typeof(ImageSet), this);
        }

        public void Delete()
        {
            if (!IsProcessed)
            {
                throw new Exception("Not all images have been processed.");
            }

            FileInfo file = null;

            try
            {
                //delete Imageset file
                file = new FileInfo(Filepath);
                if (file.Exists)
                {
                    file.Delete();
                }
            }
            catch (Exception ex)
            {
                TPDSConfig.ErrorLog.WriteError(ex.Message, Filepath);
            }

            string currentfile = String.Empty;
            try {
            //delete all images in this image set
                foreach (Image image in Images)
                {
                    image.Delete(Path.GetDirectoryName(Filepath));
                }
            }
            catch (Exception ex)
            {
                TPDSConfig.ErrorLog.WriteError(ex.Message, currentfile);
            }     
            
        }


    }

}
